<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
      <title>Everrest-Picocontainer Integration Example</title>
   </head>
   <body>
      <h1>EverRest-Picocontainer Integration Example</h1>
      <p>This is sample of integration EverRest framework with Picocontainer.</p>
      <p>We will create simple books service. It should be able give access to books by id, get list all available books 
      and add new book in storage. Service supports JSON format for transfer data to/from client.</p>
      <ul>
	      <li>You need add composer of Book application.
	      <pre>
         &lt;context-param&gt;
            &lt;param-name&gt;webapp-composer-class&lt;/param-name&gt;
            &lt;param-value&gt;org.everrest.sample.pico.BookApplicationComposer&lt;/param-value&gt;
         &lt;/context-param&gt;
	      </pre>
	      This is extension of <i>org.everrest.pico.EverrestComposer</i>. It adds JAX-RS components in corresponded scoped
	      containers.
	      <pre>
         public class BookApplicationComposer extends EverrestComposer
         {
            @Override
            protected void doComposeApplication(MutablePicoContainer container, ServletContext servletContext)
            {
               container.addComponent(BookNotFoundExceptionMapper.class);
               container.addComponent(BookStorage.class);
            }

            @Override
            protected void doComposeRequest(MutablePicoContainer container)
            {
               container.addComponent(BookService.class);
            }

            @Override
            protected void doComposeSession(MutablePicoContainer container)
            {
            }
         }
	      </pre>
	      </li>
	      <li>Then you need add EverrestPicoFilter.
	      <pre>
         &lt;filter&gt;
            &lt;filter-name&gt;picoFilter&lt;/filter-name&gt;
            &lt;filter-class&gt;org.everrest.pico.servlet.EverrestPicoFilter&lt;/filter-class&gt;
         &lt;/filter&gt;
         &lt;filter-mapping&gt;
            &lt;filter-name&gt;picoFilter&lt;/filter-name&gt;
            &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
         &lt;/filter-mapping&gt;
	      </pre></li>
	      <li>Add EverrestServlet.
	      <pre>
         &lt;servlet&gt;
            &lt;servlet-name&gt;EverrestServlet&lt;/servlet-name&gt;
            &lt;servlet-class&gt;org.everrest.core.servlet.EverrestServlet&lt;/servlet-class&gt;
         &lt;/servlet&gt;
         &lt;servlet-mapping&gt;
            &lt;servlet-name&gt;EverrestServlet&lt;/servlet-name&gt;
            &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
         &lt;/servlet-mapping&gt;
	      </pre>
	      </li>
	      <li>EverRest components.
	      <p><i>org.everrest.sample.pico.Book</i> - is simple Java Bean that will be used to transfer data via JSON.</p>
	      <p><i>org.everrest.sample.pico.BookNotFoundException</i> - exception that will be thrown by
	      <i>org.everrest.sample.pico.BookService</i> if client requested book that does not exist in storage.</p>
	      <p><i>org.everrest.sample.pico.BookNotFoundExceptionMapper</i> - JAX-RS component that intercepts
	      <i>org.everrest.sample.pico.BookNotFoundException</i> and send correct response to client.</p>
	      <pre>
         @Provider
         public class BookNotFoundExceptionMapper implements ExceptionMapper&lt;BookNotFoundException&gt;
         {
            public Response toResponse(BookNotFoundException exception)
            {
               return Response.status(404).entity(exception.getMessage()).type("text/plain").build();
            }
         }
	      </pre>
	      <p>This is component of application scope picocontainer. It minds only one instance of BookNotFoundExceptionMapper
	      will be created.</p>
	      <p><i>org.everrest.sample.pico.BookService</i> - JAX-RS service that process client's requests.
	      This is resource which inject BookStorage instance via <i>org.picocontainer.annotations.Inject</i>
	      annotation. This is component of request scope picocontainer that minds it is per-request resource.</p>
	      <pre>
         @Path("books")
         public class BookService
         {
            @org.picocontainer.annotations.Inject
            private BookStorage bookStorage;
	      ...
	      </pre>
	      <p><i>org.everrest.sample.pico.BookStorage</i> - storage of Books. This is component of application scope
	      picocontainer.</p>
	      <pre>
         public class BookStorage
         {
	      ...
	      </pre></li>
	      <li>Request mapping.
	      <table border="1"><tbody>
	      <tr>
	      <th>Relative Path</th>
         <th>HTTP Method</th>
         <th>Description</th>
	      </tr>
	      <tr>
	      <td>pico-book-service/books/{id}</td>
	      <td>GET</td>
	      <td>Get books with specified id. Just after server start only one book in storage and it can be accessed via
	      id <i>101</i></td>
	      </tr>
	      <tr>
	      <td>pico-book-service/books/</td>
	      <td>GET</td>
	      <td>Get all books from storage.</td>
	      </tr>
	      <tr>
	      <td>pico-book-service/books/</td>
	      <td>PUT</td>
	      <td>Add new book in storage. The body of request must contains book's description in JSON format. The
	      <i>Content-type</i> header must be set to <i>application/json</i></td>
	      </tr>
	      </tbody></table>
	      </li>
	      <li>How to try.
	      <p>Build project.</p>
	      <pre>mvn clean install</pre>
	      <p>Run it with Jetty server.</p>
         <pre>mvn jetty:run</pre>
         <p>Point you web browser to <a href="http://localhost:8080/pico-book-service/books/101">http://localhost:8080/pico-book-service/books/101</a></p>
         <p>If you are under linux or other unix like OS the you can use <i>curl</i> utility (often it is already installed).
         Binary build of this utility available for windows also at <a href="http://curl.haxx.se/download.html">http://curl.haxx.se/download.html</a>.
         With <i>curl</i> you able to add new book in storage via command</p>
         <pre>
         curl -X PUT \
         -H "Content-type:application/json" \
         -d '{"author":"My Author","title":"My Title","price":1.00,"pages":100}' \
         http://localhost:8080/pico-book-service/books/ 
         </pre>
	      </li>
      </ul>
   </body>
</html>